/*
 * ikakeibo 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.view.bar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

import com.wateray.ipassbook.kernel.Kernel;
import com.wateray.ipassbook.ui.LookAndFeelSelector;
import com.wateray.ipassbook.ui.StandardFrame;
import com.wateray.ipassbook.ui.action.ActionManager;
import com.wateray.ipassbook.ui.action.ExitAction;
import com.wateray.ipassbook.ui.action.ShowAboutDialogAction;
import com.wateray.ipassbook.ui.action.ShowReceiptAction;
import com.wateray.ipassbook.util.LanguageLoader;
import com.wateray.ipassbook.util.log4j.Log;

/**
 * @author wateray
 * @create 2009-4-29
 */
public class AppMenuBar extends JMenuBar {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -8344186361583588065L;
	private Logger logger = Log.getLogger(this.getClass());

	/** The file. */
	private JMenu file;

	/** The edit. */
	private JMenu edit;

	/** The view. */
	private JMenu view;

	/** The navigator */
	private JMenu navigator;

	/** The help */
	private JMenu help;

	/**
	 * Instantiates a new application menu bar.
	 */
	public AppMenuBar() {
		super();
		addMenus();
	}

	/**
	 * Creates "File" menu
	 * 
	 * @return
	 */
	private JMenu getFileMenu() {
		if (file == null) {
			file = new JMenu(LanguageLoader.getString("menu_file"));
			file.setMnemonic(KeyEvent.VK_F);
			file.add(new JMenuItem(ActionManager
					.getAction(ShowReceiptAction.class)));
			file.add(new JMenuItem(ActionManager
					.getAction(ExitAction.class)));
		}
		return file;
	}

	/**
	 * Creates "Edit" menu
	 * 
	 * @return
	 */
	private JMenu getEditMenu() {
		if (edit == null) {
			edit = new JMenu(LanguageLoader.getString("memu_edit"));
		}
		return edit;
	}

	/**
	 * Creates "View" menu
	 * 
	 * @return
	 */
	private JMenu getViewMenu() {
		if (view == null) {
			view = new JMenu(LanguageLoader.getString("memu_view"));
			// LookAndFeel
			ButtonGroup lafGroup = new ButtonGroup();
			List lf = LookAndFeelSelector.getListOfSkins();
			Iterator it = lf.iterator();
			while (it.hasNext()) {
				String lfName = it.next().toString();
				JRadioButtonMenuItem item = new JRadioButtonMenuItem();
				item.setText(lfName);
				item.setName(lfName);
				
				item.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent ae) {
						// TODO Auto-generated method stub
						  JRadioButtonMenuItem rb2 = (JRadioButtonMenuItem)ae.
	                       getSource();
						LookAndFeelSelector.setLookAndFeel(rb2.getName());
						//If is default lookAndFeel need to restart.
//						if(LookAndFeelSelector.isDefaultLookAndFeel(rb2.getName())){
//							Kernel.genInstance().restart();
//						}
						com.wateray.ipassbook.ui.ColorDefinitions.initColors();
						SwingUtilities.updateComponentTreeUI(StandardFrame.getInstance());
					}

				});
				view.add(item);
				lafGroup.add(item);
			}
			// view.add(new JMenuItem());
		}
		return view;
	}

	/**
	 * Creates "Navigator" menu
	 * 
	 * @return
	 */
	private JMenu getNavigatorMenu() {
		if (navigator == null) {
			navigator = new JMenu(LanguageLoader.getString("memu_navigator"));
		}
		return navigator;
	}

	/**
	 * Creates "Navigator" menu
	 * 
	 * @return
	 */
	public JMenu getHelpMenu() {
		if (help == null) {
			help = new JMenu(LanguageLoader.getString("Help"));
			help.setMnemonic(KeyEvent.VK_H);
			help.add(new JMenuItem(ActionManager
					.getAction(ShowAboutDialogAction.class)));
		}
		return help;
	}

	/**
	 * Adds the menus.
	 */
	private void addMenus() {
		this.add(getFileMenu());
		this.add(getEditMenu());
		this.add(getViewMenu());
		this.add(getNavigatorMenu());
		this.add(getHelpMenu());

	}

}
